<template>
  <div class="root-container">
    <div class="top-image fullscreen-top"></div>
      <div class="list">
        <div class="item">
          <span class="item-text">新闻中心</span>
          <img src="@/assets/images/support-bg.png" alt="" class="item-icon"/>
        </div>
      </div>
 
    <div class="tabs-container">
      <el-tabs v-model="activeName" class="right-tabs" @tab-click="handleTabClick">
        <el-tab-pane
          v-for="tab in tabs"
          :key="tab.name"
          :label="tab.label"
          :name="tab.name"
        ></el-tab-pane>
      </el-tabs>
    </div>

    <div class="party-building">
      <div class="title-container">
        <h1 class="chinese-title">{{ currentTab.label }}</h1>
        <h2 class="english-title">{{ currentTab.englishTitle }}</h2>
      </div>
      <div class="clear"></div>
      <div class="activity-list">
        <div class="activity-item" v-for="item in news" :key="item.id" 
        @click="goToDetail(item.id)" style="cursor: pointer;">
          <div class="news-content-wrapper">
            <div class="news-date">
              <div class="news-day">{{ formatDay(item.publishTime) }}</div>
              <div class="news-month-year">
                {{ formatMonthYear(item.publishTime) }}
              </div>
            </div>
            <div class="news-image">
              <img :src="item.coverUrl" alt="活动图片" class="activity-img">
            </div>
            <div class="news-text">
              <h3>{{ item.title }}</h3>
              <i></i>
              <p class="desc">{{ item.content }}</p>
            </div>
          </div>
        </div>
      </div>
   </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import {listNews} from '@/api/newsSys/news.js'

const router = useRouter()
const route = useRoute()
const news = ref([])
const loading = ref(false)
const isMobile = ref(false)

const tabs = [
  { name: 'partyBuilding', label: '党建学习', englishTitle: 'PARTY BUILDING STUDY', path: '/news/partyBuilding' },
  { name: 'newsBulletin', label: '要闻速递', englishTitle: 'NEWS', path: '/news/newsBulletin' },
  { name: 'researchAchievements', label: '科研成果', englishTitle: 'SCIENTIFIC RESEARCH ACHIEVEMENTS', path: '/news/researchAchievements' },
  { name: 'notices', label: '通知公告', englishTitle: 'NOTICES', path: '/news/notices' },
]

const activeName = ref('newsBulletin')

const fetchNews = async () => {
  loading.value = true;
  try {
    const res = await listNews({ categoryId: 2 });
    news.value = res.rows;
    console.log(res)
  } catch (err) {
    console.error('获取信息失败：', err);
  } finally {
    loading.value = false;
  }
};

onMounted(() => {
  fetchNews()
  const currentPath = route.path;
  const matchedTab = tabs.find(tab => tab.path === currentPath);
  if (matchedTab) {
    activeName.value = matchedTab.name;
  }
});

const currentTab = computed(() => {
  return tabs.find(tab => tab.name === activeName.value) || tabs[0];
});

const handleTabClick = (tab) => {
  const tabName = tab.props.name
  const targetTab = tabs.find(t => t.name === tabName)
  if (targetTab && targetTab.path) {
    router.push(targetTab.path)
  }
};

const formatDay = (dateStr) => {
  if (!dateStr) return '--'
  const date = new Date(dateStr)
  return date.getDate().toString().padStart(2, '0')
}

const formatMonthYear = (dateStr) => {
  if (!dateStr) return '--'
  const date = new Date(dateStr)
  const year = date.getFullYear()
  const month = (date.getMonth() + 1).toString().padStart(2, '0')
  return `${year}-${month}`
}

const formatYear = (dateStr) => {
  if (!dateStr) return '--'
  const date = new Date(dateStr)
  return date.getFullYear()
}

const goToDetail = (id) => {
  router.push({
    name: 'NewsDetail',
    params: { id }
  })
}
</script>

<style scoped>
/* ==================== 顶部图片区域 ==================== */
.top-image {
  width: 100%;
  height: 260px;
  background-image: url("@/assets/images/support-bg.jpg");
  background-position: center;
  border-radius: 0 0 20px 20px;
  box-shadow: 0 6px 12px rgba(183, 28, 28, 0.25);
}

/* ==================== 标题区域 ==================== */
/* 顶部标题 */
.list {
  display: flex;
  align-items: center;
  position: relative;
  z-index: 0;
}
.list::after {
  content: '';
  position: absolute;
  left: 0;
  bottom: -20px;
  width: 100%;
  height: 90px;
  background-color: #eae4e4;
  z-index: 0;
}
.list .item {
  position: relative;
  margin-top: -30px;
  z-index: 1;
}
.list .item-icon {
  display: block;
  position: relative;
  z-index: 1;
}
.list .item-text {
  position: absolute;
  left: 50%;
  top: 25%;
  transform: translateX(-50%);
  font-size: 32px;
  color: #fff;
  font-weight: bold;
  text-align: center;
  z-index: 2;
  text-shadow: 2px 2px 6px rgba(0, 0, 0, 0.3);
}

.tabs-container {
  width: 100%;
  display: flex;
  justify-content: center;
  padding: 50px 10px;
}

.right-tabs {
  font-size: 50px !important;
}

:deep(.el-tabs__item) {
  font-family: "自定义字体名", "微软雅黑", sans-serif !important;
  font-size: 24px !important;
  padding: 2px 2px 4px !important; 
  margin: 0 24px !important;
  position: relative !important;
  transition: color 0.3s ease !important;
  background: transparent !important;
  border: none !important;
  display: inline-block !important;
  white-space: nowrap !important;
}

:deep(.el-tabs__item:hover) {
  color: #9a0001 !important;
  font-weight: 600 !important;
}

:deep(.el-tabs__item:hover::after) {
  content: "" !important;
  position: absolute !important;
  bottom: 0 !important;
  left: 0 !important;
  right: 0 !important;
  height: 4px !important;
  background-color: #9a0001 !important;
  transition: all 0.3s ease !important;
}

:deep(.el-tabs__item.is-active) {
  color: #9a0001 !important;
  font-weight: 600 !important;
}

:deep(.el-tabs__item.is-active::after) {
  content: "" !important;
  position: absolute !important;
  bottom: 0px !important;
  left: 0 !important;
  right: 0 !important;
  height: 4px !important;
  background-color: #9a0001 !important;
}

:deep(.el-tabs__active-bar) {
  display: none !important;
}

:deep(.el-tabs__nav-wrap::after) {
  display: none !important;
}

.party-building{
  padding: 30px 40px;
  margin: 0 auto;
}

.title-container {
  padding-left: 10px;
  position: relative; 
  width: 100%;
  height: 100px;
  float: left;
}

.title-container::before{
  content: "";
  position: absolute;
  left: 0;
  top: 10%;
  background: #9a0001;
  width: 5px;
  height: 70%;
}

.chinese-title {
  color: #9a0001;
  font-size: 32px;
  margin-top: 0px;
  margin-bottom: 5px;
  margin-left: 30px;
}

.english-title {
  font-size: 22px;
  color: #bfbfbf;
  font-weight: bold;
  margin-top: 0px;
  margin-left: 30px;
}

.clear{
  clear: both;
}

.activity-list {
  display: grid;
  grid-template-columns: 1fr;
  gap: 24px;
  margin-top: 0px;
  padding: 60px 0px;
  overflow-x: hidden !important
}

.activity-item {
  width: 95%;
  /* max-width: 1284px; */
  margin: 0 auto;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
  transition: box-shadow 0.3s ease;
  margin-bottom: 12px;
  cursor: pointer;
}

@media (max-width: 840px) {
  .news-content-wrapper {
    flex-direction: column;
  }

  .news-date {
    width: 20%;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
  }

  .news-day {
    font-size: 32px;
    font-weight: bold;
    color: #9a0001;
    line-height: 2;
  }

  .news-month-year {
    font-size: 16px;
    color: #9a0001;
    text-align: center;
    line-height: 1.2;
  }

  .news-image {
    width: 100%;
    height: 200px;
  }
}

.news-content-wrapper {
  display: flex;
  align-items: stretch;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
  transition: box-shadow 0.3s ease;
}

.news-date {
  width: 16%;
  background-color: #f0f0f0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 15px;
}

.news-day {
  font-size: 32px;
  font-weight: bold;
  color: #9a0001;
  line-height: 2;
}

.news-month-year {
  font-size: 16px;
  color: #9a0001;
  text-align: center;
  line-height: 1.2;
}

.news-image {
  width: 250px;
  flex-shrink: 0;
}

.activity-item:hover {
  /* box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1); */
  background-color: #9a0001;
  color: white;
  box-shadow: 0 6px 16px rgba(154, 0, 1, 0.15);
}


.activity-item:hover h3,
.activity-item:hover p{
  color: white;
}

.activity-img {
  width: 100%;
  height: 180px;
  object-fit: cover;
}

.news-text {
  flex-grow: 1;
  padding: 15px;
  width: 100%;
  float: left;
}

h3 {
  font-size: 20px;
  font-weight: 700;
  color: #374151;
  padding: 10px;
  margin: 0;
}
.news-text i {
    display:block;
    /* float: left; */
    width: 35px;
    height: 2px;
    background: #be9e5d;
    margin: 0px 9px 15px;
}
.date {
  color: #6b7280;
  font-size: 14px;
  padding: 0 10px 8px;
  margin: 0;
}

.desc {
  color: #9ca3af;
  font-size: 14px;
  line-height: 28px;
  padding: 0 10px 6px;
  margin: 0;
  width: 95%;
  height: 56px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  word-break: break-all;
}
</style>
